package com.bwie.contract.controller;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.domain.Contract;
import com.bwie.common.domain.request.ContractRequest;
import com.bwie.common.result.Result;
import com.bwie.contract.service.ContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
public class ContractController {

    @Autowired
    private ContractService contractService;

    @Autowired
    private HttpServletRequest httpServletRequest;

    /**
     * 查询合同数量
     * @return
     */
    @GetMapping("/selectContractQuantity")
    public Result<List<Contract>> selectContractQuantity(){
        log.info("功能:查询合同数量");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        List<Contract> list = contractService.selectContractQuantity();
        log.info("功能:查询合同数量");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        log.info("响应结果:{}"+ JSONObject.toJSONString(list));
        return Result.success(list);
    }


    /**
     * 查询合同
     * @param contract
     * @return
     */
    @PostMapping("/queryContract")
    public Result<List<Contract>> queryContract(@RequestBody ContractRequest contract){
        log.info("功能:查询合同");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        List<Contract> contracts = contractService.queryContract(contract);
        log.info("功能:查询合同");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        log.info("响应结果:{}"+ JSONObject.toJSONString(contracts));
        return Result.success(contracts);
    }

    @PostMapping("/saveContract")
    public Result<Integer> saveContract(@RequestBody Contract contract){
        Integer i = contractService.saveContract(contract);
        return Result.success(i);
    }

    @PostMapping("/updateContract")
    public Result<Integer> updateContract(@RequestBody Contract contract){
        Integer i = contractService.updateContract(contract);
        return Result.success(i);
    }
}
